// 相对路径
import actionFn from './wf.action';
import actionRow from './wf-fn.op.ko';

import opFn from './wf.op.fn';

import yQueue from './wf.y.queue';
import yForm from './wf.y.form';
import yAcl from './wf.y.acl';

import uiFn from './wf.ui.fn';

import uiWebFn from './wf-fn.ui.component';
import Ux from 'ux';

// eslint-disable-next-line import/no-anonymous-default-export
export default (reference, node) => {
    /*
     * 基础流程规范，属性中必须包含 $workflow
     * {
     *      "parameter":
     * }
     */
    return ({
        rxClose: opFn.rxCloseFn(reference),
        /*
         * 打开行相关操作（行打开时需重新读取工作流）
         */
        rxRow: opFn.rxRowFn(reference),
        // ======================= 函数区域 =======================
        Jsx: {
            ...uiWebFn(reference),
        },
        // ======================= Jsx组件定制 =======================
        /* 列表定制 */
        JsxList: uiFn.jsxListFn(reference),
        // 行为提交专用，流程驱动
        Act: actionFn(reference, node),
        // ======================= 复杂字段配置 =======================
        JsxComplex: {
            monitorContainer: {
                /*
                 * - ticket：关联工单
                 * - bpmn：流程信息（必须）
                 * - linkageAttachment      - 关联附件
                 * - linkageAsset           - 关联资产
                 * - linkageEmployee        - 关联员工
                 */
                koTab: ($items = []) => {
                    const {$workflow = {}} = reference.props;
                    const {config = {}} = $workflow;
                    const linkageMap = config.linkage ? config.linkage : {};
                    // 默认 key 值
                    const enabled = [
                        "ticket",                   // 主单据
                        "bpmn",                     // 流程图
                        "linkageAttachment",        // 附件
                    ]
                    const {linkage = []} = config.ui ? config.ui : {};
                    linkage.forEach(name => {
                        if (linkageMap.hasOwnProperty(name)) {
                            enabled.push(name);
                        }
                    })
                    const normalized = [];
                    enabled.forEach(item => {
                        const found = Ux.elementUnique($items, 'key', item);
                        if (found) {
                            normalized.push(found);
                        }
                    })
                    return normalized;
                }
            }
        },
        /*
         * 表单定制
         */
        JsxForm: {
            __children: {
                phase: uiFn.jsxPhaseFn(reference),
                // 操作历史专用字段
                monitorHistory: uiFn.jsxHistoryFn(reference),
                // 流程图专用字段
                monitorBpmn: uiFn.jsxBpmnFn(reference),
                // 关联工单
                linkageTicket: uiFn.jsxLinkageFn(reference, "linkageTicket"),
                // 关联资产
                linkageAsset: uiFn.jsxLinkageFn(reference, "linkageAsset"),
                // 关联员工
                linkageEmployee: uiFn.jsxLinkageFn(reference, "linkageEmployee"),
            }
        },
        // ======================= 列表组件 =======================
        ...yQueue(reference),

        // ======================= 表单组件 =======================
        ...yForm(reference),

        // acl 控制
        ...yAcl(reference),

        // ======================= Ko 插件专用 =======================
        yoPlugins: ($workflow) => actionRow(reference, $workflow)
    })
}